package com.apps.ftapp.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.SubjectMoreGrid2Adapter;
import com.apps.ftapp.adapter.SubjectMoreGridAdapter;
import com.apps.ftapp.model.SubjectMoreModel;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.BroadCastManager;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.SharedPreferencesUtil;
import com.apps.ftapp.util.Singleton;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;

public class SubjectMore2Activity extends BaseActivity {
    //
    @BindView(R.id.gridView)
    GridView gridView;
    @BindView(R.id.gridView1)
    GridView gridView1;


    private final static String ACTION_NAME = "ACTION_NAME";

    private SubjectMoreGridAdapter subjectMoreGridAdapter;
    private List<String> gridList = new ArrayList();


    private SubjectMoreGrid2Adapter subjectMoreGrid2Adapter;
    private List<Map> gridList2 = new ArrayList();

    private List<String> gsList = new ArrayList<>();

    private Gson gson = new Gson();

    String category = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subject_more2);
        ButterKnife.bind(this);

        setTitle("选择学科");

        gridList = Singleton.getInstance().getTitList();
        category = Singleton.getInstance().getCategory();

        Log.i("main", ":::" + category);

        initView();

        loadData();


    }


    private void loadData() {

        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("page", 1);
        Async.post("course/category", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    //SubjectModel
                    SubjectMoreModel result = gson.fromJson(new String(responseBody), SubjectMoreModel.class);
                    if (1 == Tools.isIntNull(result.getSuccess())) {

                        Log.i("main", "...............");
                        //
                        if ("1".equals(Tools.isNull(category))) {
                            //资格
                            if (null != result.getInfo().getZgzxueke() && result.getInfo().getZgzxueke().size() > 0) {
                                for (SubjectMoreModel.InfoBean.ZgzxuekeBean z : result.getInfo().getZgzxueke()) {
                                    Map m = new HashMap();
                                    m.put("id", z.getId());
                                    m.put("name", z.getName());
                                    gsList.add(z.getName());
                                    gridList2.add(m);
                                }
                            }
                        } else {
                            //招聘
                            for (SubjectMoreModel.InfoBean.ZpxuekeBean z : result.getInfo().getZpxueke()) {
                                Map m = new HashMap();
                                m.put("id", z.getId());
                                m.put("name", z.getName());
                                gsList.add(z.getName());
                                gridList2.add(m);
                            }
                        }
                        Log.i("main", "..............." + gridList2);
                        subjectMoreGrid2Adapter.addRes(gsList);

                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ACTION_NAME)) {
                String msg = intent.getStringExtra("msg");
                Toasty.normal(getApplicationContext(), "111111111111111").show();
//                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }

        }
    };

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTION_NAME);
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }


    private void initView() {

        subjectMoreGridAdapter = new SubjectMoreGridAdapter(getApplicationContext(), gridList);
        gridView.setAdapter(subjectMoreGridAdapter);


        subjectMoreGrid2Adapter = new SubjectMoreGrid2Adapter(getApplicationContext(), gsList);
        gridView1.setAdapter(subjectMoreGrid2Adapter);
        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Toasty.normal(getApplicationContext(),"::"+i).show();

                Map m = new HashMap();
                m.put("id", gridList2.get(i).get("id"));
                m.put("name", gridList2.get(i).get("name"));
                Singleton.getInstance().setSanji(m);
                SharedPreferencesUtil.saveData(getApplicationContext(), "sanjiId", gridList2.get(i).get("id"));
                SharedPreferencesUtil.saveData(getApplicationContext(), "sanjiName", gridList2.get(i).get("name"));

                L.i("main", "Constant.IS_EDIT_CHANAGE::::" + Constant.IS_EDIT_CHANAGE);
                if (Constant.IS_EDIT_CHANAGE.equals("UserInfoActivity")) {
                    Intent intent = new Intent();
                    intent.putExtra("order", "111111111");
                    intent.setAction("UserInfoActivity");
                    L.i("main", "Constant.IS_EDIT_CHANAGE::::userinfo");
                    BroadCastManager.getInstance().sendBroadCast(SubjectMore2Activity.this, intent);
                } else if (Constant.IS_EDIT_CHANAGE.equals("IndexFragment")) {
                    //发送广播
                    Intent intent = new Intent();
                    intent.putExtra("order", "111111111");
                    intent.setAction("fragment_home_left");
                    L.i("main", "Constant.IS_EDIT_CHANAGE::::首页");
                    BroadCastManager.getInstance().sendBroadCast(SubjectMore2Activity.this, intent);

                } else if (Constant.IS_EDIT_CHANAGE.equals("SubjectFragment")) {
                    Intent intent = new Intent();
                    intent.putExtra("order", "111111111");
                    intent.setAction("SubjectFragment");
                    L.i("main", "Constant.IS_EDIT_CHANAGE::::题库");
                    BroadCastManager.getInstance().sendBroadCast(SubjectMore2Activity.this, intent);

                }


                upds();


                Toasty.normal(getApplicationContext(), "更新成功").show();
                finish();
//                ActivitySkipUtil.skipAnotherActivity(SubjectMore2Activity.this, GestureFilpActivity.class,data);

            }
        });

    }


    //settings/ks_type_update


    private void upds() {

        String xueke = Tools.isNull(SharedPreferencesUtil.getData(getApplicationContext(), "sanjiId", ""));
        String xueduan = Tools.isNull(SharedPreferencesUtil.getData(getApplicationContext(), "erjiId", ""));
        String category = Tools.isNull(SharedPreferencesUtil.getData(getApplicationContext(), "yijiId", ""));

        L.i("main", "xueke::::" + xueke);
        L.i("main", "xueduan::::" + xueduan);
        L.i("main", "category::::" + category);

        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("category", category);
        params.put("xueduan", xueduan);
        params.put("xueke", xueke);
        Async.post("settings/ks_type_update", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", ">>>>>" + new String(responseBody));

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                L.i("main", ">>>>>" + new String(responseBody));
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unregisterReceiver(mBroadcastReceiver);
    }
}
